package com.neusoft.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.neusoft.domain.FoodCategory;
import com.neusoft.domain.PageResult;
import com.neusoft.mapper.FoodCategoryMapper;
import com.neusoft.mapper.FoodMapper;
import com.neusoft.service.FoodCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class FoodCategoryServiceImpl implements FoodCategoryService {
    @Autowired
    private FoodCategoryMapper foodCategoryMapper;
    @Autowired
    private FoodMapper foodMapper;
    @Override
    public PageResult<FoodCategory> pageSelectFoodCategory(Integer pageNum, Integer pageSize, String categoryName, Integer shopId) {
        PageHelper.startPage(pageNum,pageSize);
        Page<FoodCategory>page = (Page<FoodCategory>)foodCategoryMapper.pageSelectFoodCategory(categoryName,shopId);
        System.out.println(page.getResult());
        System.out.println(page.getTotal());
        PageResult<FoodCategory> pageResult = new PageResult<>(page.getTotal(),page.getResult());
        return pageResult;
    }

    @Override
    public void saveCategory(FoodCategory foodCategory) {
        foodCategoryMapper.saveCategory(foodCategory);
    }

    @Override
    public void updateCategory(FoodCategory foodCategory) {
        foodCategoryMapper.updateCategory(foodCategory);
    }

    @Override
    public void deleteCategory(Integer categoryId) {
        if(categoryId==null){
            throw new RuntimeException("参数异常");
        }
        Integer count =foodMapper.selectFoodByCategoryId(categoryId);
        if(count>0){
            throw new RuntimeException("该分类下有菜品，不能删除");
        }
        foodCategoryMapper.deleteCategory(categoryId);
    }
}
